package com.nhjf.mobile.controller.teacher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nhjf.common.page.Pagination;
import com.nhjf.mobile.common.util.SysProperties;
import com.nhjf.model.teachers.ReviewPainting;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.model.teachers.TeacherHasCoursesType;
import com.nhjf.pojo.dto.teachers.TeacherInformationDto;
import com.nhjf.pojo.qo.teachers.ReviewPaintingQo;
import com.nhjf.pojo.qo.teachers.TeacherHasCoursesTypeQo;
import com.nhjf.pojo.qo.teachers.TeachersQo;
import com.nhjf.service.teachers.ReviewPaintingService;
import com.nhjf.service.teachers.TeacherHasCoursesTypeService;
import com.nhjf.service.teachers.TeachersService;

import net.sf.json.JSONObject;

@RequestMapping("/teacher")
@Controller
public class TeacherController {
	@Autowired
	private TeachersService teachersService;
	@Autowired
	private TeacherHasCoursesTypeService teacherHasCoursesTypeService;
	@RequestMapping("/teacher_queryAll")
	@ResponseBody
	public String queryTeacher(@RequestParam(value = "page", defaultValue = "1")Integer page) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			List<TeacherInformationDto> informationDtos=new ArrayList<TeacherInformationDto>();
			TeachersQo teachersQo=new TeachersQo();
			teachersQo.setSeachUser(true);
			teachersQo.setOrderOpenRedraw(1);
			Pagination pagination = new Pagination();
			pagination.setPageSize(10);
			pagination.setPageNo(page);
			pagination.setCondition(teachersQo);
			pagination = teachersService.queryPagination(pagination);
			List<Teacher> teacherList=(List<Teacher>)pagination.getList();
			SysProperties sysProperties=SysProperties.getInstance();
			String imagePath=sysProperties.get("imagePath");
			for (Teacher teacher : teacherList) {
				TeacherInformationDto dto=new TeacherInformationDto();
				teacher.getUser().setUserPassword("");
				if(!StringUtils.isEmpty(teacher.getAvatar())) {
					if(teacher.getAvatar().indexOf(imagePath+"/picture/")>=0) {

					}else {
						dto.setAvatar(imagePath+"/picture/"+teacher.getAvatar());
					}
				}
				TeacherHasCoursesTypeQo teacherHasCoursesTypeQo1=new TeacherHasCoursesTypeQo();
				teacherHasCoursesTypeQo1.setTeacherId(teacher.getId());
				teacherHasCoursesTypeQo1.setSeachCoursesType(true);
				List<TeacherHasCoursesType> typeList=teacherHasCoursesTypeService.queryList(teacherHasCoursesTypeQo1);
				List<String> typeListString=new ArrayList<String>();
				for (TeacherHasCoursesType cType : typeList) {
					typeListString.add(cType.getCoursesTypeId().getCoursesType());
				}
				dto.setOpenRedraw(teacher.getOpenRedraw());
				dto.setUserId(teacher.getUser().getId());
				dto.setListType(typeListString);
				dto.setNickName(teacher.getNickname());
				informationDtos.add(dto);
			}
			pagination.setList(informationDtos);
			if(teacherList!=null) {
				resMap.put("code", "200");
				resMap.put("message", pagination);
				return JSONObject.fromObject(resMap).toString();
			}else {
				resMap.put("code", "500");
				resMap.put("message", "没有老师！");
				return JSONObject.fromObject(resMap).toString();
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "系统异常！");
			return JSONObject.fromObject(resMap).toString();
		}
	}
	
	@RequestMapping("/teacher_appointment")
	@ResponseBody
	public String appointmentTeacher(@RequestParam(value = "page", defaultValue = "1")Integer page) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			List<TeacherInformationDto> informationDtos=new ArrayList<TeacherInformationDto>();
			TeachersQo teachersQo=new TeachersQo();
			teachersQo.setSeachUser(true);
			teachersQo.setOrderOpenTutor(1);
			teachersQo.setOrderString("coachTimes");
			Pagination pagination = new Pagination();
			pagination.setPageSize(10);
			pagination.setPageNo(page);
			pagination.setCondition(teachersQo);
			pagination = teachersService.queryPagination(pagination);
			List<Teacher> teacherList=(List<Teacher>)pagination.getList();
			SysProperties sysProperties=SysProperties.getInstance();
			String imagePath=sysProperties.get("imagePath");
			for (Teacher teacher : teacherList) {
				TeacherInformationDto dto=new TeacherInformationDto();
				teacher.getUser().setUserPassword("");
				if(!StringUtils.isEmpty(teacher.getAvatar())) {
					if(teacher.getAvatar().indexOf(imagePath+"/picture/")>=0) {

					}else {
						dto.setAvatar(imagePath+"/picture/"+teacher.getAvatar());
					}
				}
				TeacherHasCoursesTypeQo teacherHasCoursesTypeQo1=new TeacherHasCoursesTypeQo();
				teacherHasCoursesTypeQo1.setTeacherId(teacher.getId());
				teacherHasCoursesTypeQo1.setSeachCoursesType(true);
				List<TeacherHasCoursesType> typeList=teacherHasCoursesTypeService.queryList(teacherHasCoursesTypeQo1);
				List<String> typeListString=new ArrayList<String>();
				for (TeacherHasCoursesType cType : typeList) {
					typeListString.add(cType.getCoursesTypeId().getCoursesType());
				}
				dto.setUserId(teacher.getUser().getId());
				dto.setListType(typeListString);
				dto.setNickName(teacher.getNickname());
				dto.setOpenTutor(teacher.getOpenTutor());
				dto.setCoachTimes(teacher.getCoachTimes());
				informationDtos.add(dto);
			}
			pagination.setList(informationDtos);
			if(teacherList!=null) {
				resMap.put("code", "200");
				resMap.put("message", pagination);
				return JSONObject.fromObject(resMap).toString();
			}else {
				resMap.put("code", "500");
				resMap.put("message", "没有老师！");
				return JSONObject.fromObject(resMap).toString();
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "系统异常！");
			return JSONObject.fromObject(resMap).toString();
		}
	}
}
